<template>
  <div>
    <el-container>
      <myAside></myAside>
      <el-container>
        <el-header>
          <myHeader></myHeader>
        </el-header>
        <el-main>
          <myMian></myMian>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
import myAside from "@/views/managehome/aside/Aside.vue";
import myHeader from "@/views/managehome/header/Header.vue";
import myMian from "@/views/managehome/main/Main.vue";

// 引入messsage
import { Message } from "element-ui";
import { getUserInfo } from "@/api/user.js";
export default {
  name: "myManagehome",
  data(){
    return {
      userInfo:{
        username:'',
        profile:'',
        identity:''
      }
    }
  },
  components: {
    myAside,
    myHeader,
    myMian
  },
  methods:{
    async getUserInfoo(){
        // 如果没有用户信息，就发送请求获取用户信息，从redis获取
        await getUserInfo()
        .then(res => {
          if (res.data.code == 1000) {
            // 存入vuex
            this.$store.commit('UserInfo/SETUSERINFO',res.data.userInfo)
            // 存入本地
            localStorage.setItem('userInfo',JSON.stringify(res.data.userInfo))
            // 拿出数据
            // 身份识别
            const userInfo = JSON.parse(localStorage.getItem('userInfo')) 
            this.userInfo.username = userInfo.username
            this.userInfo.profile = userInfo.profile
            if(userInfo.identity == 0){
            this.userInfo.identity = '超管'
            }else if(userInfo.identity == 1){
            this.userInfo.identity = '管理员'
            }else if(userInfo.identity == 2){
            this.userInfo.identity = '用户'
            }else if(userInfo.identity == 3){
            this.userInfo.identity = '游客'
            }
          } else {
            this.$router.push("/login");
          }
        })
        .catch(err => {
          console.log(err);
        });
      
    }
  },
  async created() {
    await this.getUserInfoo()
  },
  // 组件路由首位
  beforeRouteEnter(to, from, next) {
    const token = localStorage.getItem('token')
    if(token){
      next()
    }else{
      Message.error('未登录')
      next('/login')
    }
  }
};
</script>
<style lang="less" scoped>
.el-header {
  background-color: #233b59;
  color: rgb(231, 227, 227);
  line-height: 60px;
}

.el-aside {
  color: #333;
  text-align: center;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
}
</style>